import requests
import user_agent_get
from pyquery import PyQuery as pq
from urllib.parse import urlencode
from time import sleep

session=requests.Session()
headers={"user-agent":user_agent_get.user_agent_get()
         ,"Host":"cas.pxc.jx.cn",
        "Sec-Fetch-User": '?1','Upgrade-Insecure-Requests': '1',
            'Upgrade-Insecure-Requests': '1',
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
         }
url="https://cas.pxc.jx.cn/lyuapServer/login"
resp=session.get(url,headers=headers)

html_set_cookie = requests.utils.dict_from_cookiejar(resp.cookies)
print("first get cookies :"+str(html_set_cookie))
tempPQ=pq(resp.text)
print("=====")
tempPQStr=tempPQ("input[type='hidden'][name='lt']").attr("value")
tempPQStr22=tempPQ("input[type='hidden'][name='execution']").attr("value")

print(tempPQStr)

imgUrl="https://cas.pxc.jx.cn/lyuapServer/captcha.jsp"
imgResp=session.get(imgUrl,headers=headers)
with open(r'爬虫信息/a2.jpg','wb') as f:
    f.write(imgResp.content)


data={
'username': '211319023',
'password': '000000',
'captcha': '5454',
'warn': 'true',
'lt': 'LT-11972-eoF3b1RwEIudHnOjJTS112wz7HLfWA-cas01.example.org',
'execution': 'e1s1',
'_eventId': 'submit',
'submit': '登录'
}

# https://cas-443.vpn.pxc.jx.cn/lyuapServer/login?service=https%3A%2F%2Fvpn.pxc.jx.cn%2Fusers%2Fauth%2Fcas%2Fcallback%3Furl%3Dhttp%253A%252F%252Fwww.pxc.jx.cn%252F
# https://cas-443.vpn.pxc.jx.cn/lyuapServer/login?service=https%3A%2F%2Fvpn.pxc.jx.cn%2Fusers%2Fauth%2Fcas%2Fcallback%3Furl%3Dhttp%253A%252F%252Fwww.pxc.jx.cn%252F
postUrl="https://cas.pxc.jx.cn/lyuapServer/login"
str2=input("输入验证码: ")
print(tempPQStr22)
data.update({'captcha':str2,'lt':tempPQStr,'execution':tempPQStr22})


data=urlencode(data)
print(data)
headers.update({'Referer': 'https://cas.pxc.jx.cn/lyuapServer/login','Host':'cas.pxc.jx.cn'
                ,'Origin': 'https://cas.pxc.jx.cn','Content-Type': 'application/x-www-form-urlencoded'})
print("headers = "+str(headers))
sepost=session.post(url=postUrl,headers=headers,data=data,allow_redirects=False)
print("post Status: "+str(sepost.status_code))
print("++++++++++++++++++++")
print(sepost.text)
print("++++++++++++++++++++++++")

print('---------------')
print(sepost.text)
print("post headers: "+str(sepost.headers))
mycook=session.cookies
# html_set_cookie = requests.utils.dict_from_cookiejar(session.cookies)
print("cookies "+str(mycook.get_dict()))


print('****************************************')
url2=sepost.headers['Location']
headers.update({"Host":"portal.pxc.jx.cn"})
urlans=session.get(url=url2,headers=headers,allow_redirects=False)
print("url2 get "+str(urlans.status_code))
print(urlans.text)
print("url2 get headers "+str(urlans.headers))

mycook=session.cookies
print("cookies "+str(mycook.get_dict()))


sleep(0.5)
print('#####################################')
url3=urlans.headers['Location']
headers.update({'Host':"cas.pxc.jx.cn",'Upgrade-Insecure-Requests': '1'
                ,'Cache-Control': 'max-age=0','Connection': 'keep-alive'})
headers.pop('Referer')
headers.pop('Sec-Fetch-User')
headers.pop('Origin')
print("###"+str(headers))
print(url3)
urlans2=session.get(url=url3,headers=headers,allow_redirects=False)
print("url3 get "+str(urlans2.status_code))
print(urlans2.text)
print("url3 get headers "+str(urlans2.headers))
mycook=session.cookies
print("cookies "+str(mycook.get_dict()))
print(urlans2.text)



sleep(0.5)
print('+++++++++++++++++++++++++++++++++++++++++++')
url4=urlans2.headers['Location']
print("url4 = "+str(url4))
print("url4 headers "+str(headers))
headers.update({"Host":"cas.pxc.jx.cn",'Upgrade-Insecure-Requests': "1",
                'Sec-Fetch-User': '?1'})
urlans3=session.get(url=url4,headers=headers,allow_redirects=False)
print("url4 get "+str(urlans3.status_code))
print(urlans3.text)
print("url4 get headers "+str(urlans3.headers))

mycook=session.cookies
print("cookies "+str(mycook.get_dict()))


print('=====================================')
headers.update({"Host": "portal.pxc.jx.cn"})
url5=urlans3.headers['Location']
urlans4=session.get(url=url5,headers=headers,allow_redirects=False)
print("url5 get "+str(urlans4.status_code))
print("url5 get headers "+str(urlans4.headers))


# 最后返回的页面
url6=urlans4.headers['Location']
fina_1=session.get(url=url6,headers=headers,allow_redirects=False)
print(fina_1.text)


url7="http://jw.pxc.jx.cn/jsxsd/grxx/xsxx"
fina_2=session.get(url=url7)
mypq=pq(fina_2.text)
print(mypq.html())
with open("test3.html",'w',encoding='utf-8') as f:
    f.write(fina_2.text)
